package game.engine.base.anim.single

import game.engine.base.Node
import game.engine.base.anim.Tween

open class RotateTo(val duration: Float, var dstDegress: Float) : Tween() {

    var firstExecute = false

    var px = 0f
    var cx = 0f
    var elapsedTime = 0f

    override fun initialize(node: Node) {
        cx = node.degress
        px = (dstDegress - node.degress) / duration
        elapsedTime = 0f
    }

    override fun execute(dt: Float) {
        val node = target!!
        if (!firstExecute) {
            initialize(node)
            firstExecute = true
        }

        elapsedTime += dt
        if (elapsedTime >= duration) {
            node.degress = dstDegress
            end = true
        } else {
            cx += px * dt
            node.degress = cx
        }
    }

    override fun clone(): Tween {
        return RotateTo(duration,dstDegress)
    }
}

class RotateBy(duration: Float, val dxDegress: Float) : RotateTo(duration,0f) {
    override fun initialize(node: Node) {
        cx = node.degress
        dstDegress = dxDegress + cx
        px = dxDegress / duration
        elapsedTime = 0f
    }

    override fun clone(): Tween {
        return RotateBy(duration,dxDegress)
    }
}
